package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.MarketStorage;
import com.cskaoyan.model.admin.bo.AdminCreateBo;
import com.cskaoyan.model.admin.bo.BaseParam;
import com.cskaoyan.model.admin.vo.BaseData;
import com.cskaoyan.model.admin.vo.BaseRespVo;
import com.cskaoyan.service.admin.StorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @author hanshuai
 * @version 1.0
 * @description @TODO
 * @date 2021/11/29 20:53
 */
@RestController
@RequestMapping("admin/storage")
public class AdminStorageController {
    @Autowired
    StorageService storageService;

    @RequestMapping("create")
    public BaseRespVo adminCreate(MultipartFile file) throws IOException {
        UUID uuid = UUID.randomUUID();
        String originalFilename = file.getOriginalFilename();
        UUID code = uuid;
        file.transferTo(new File("d:/wx/storage/fetch/", uuid + originalFilename.substring(originalFilename.length() - 4)));
        MarketStorage marketStorage = storageService.storeAvatar(file, code);
        return BaseRespVo.ok(marketStorage);
    }

    public void deleteStorage(AdminCreateBo adminCreateBo) {
        storageService.deleteStorage(adminCreateBo);
    }

    @RequestMapping("list")
    public BaseRespVo storageList(BaseParam baseParam, String key, String name) {
        BaseData baseData = storageService.storageList(baseParam, key, name);
        return BaseRespVo.ok(baseData);
    }

    @RequestMapping("update")
    public BaseRespVo storageUpdate(@RequestBody MarketStorage marketStorage) {
        MarketStorage marketStorageVo = storageService.updateStorage(marketStorage);
        return BaseRespVo.ok(marketStorageVo);
    }

    @RequestMapping("/delete")
    public BaseRespVo storageDelete(@RequestBody MarketStorage marketStorage) {
        storageService.deleteStorage(marketStorage);
        return BaseRespVo.ok(null);
    }
}
